import log from './log.js';
import sign from './sign.js';
import store from '@/store/index.js';
import config from '@/config.js'
const sendRequest = function(param, useToken = true) {
	var _self = this,
		url = config.domain + param.url,
		method = param.method || 'POST',
		header = {
			'content-type': "application/x-www-form-urlencoded"
		},
		data = param.data || {},
		token = store.state.token || '',
		// token = '1cvd52i2083b6b1bsd9i41fcn5',
		hideLoading = param.hideLoading || false;
	if (useToken) {
		header['token'] = token;
	}
	//固定参数:仅仅在小程序绑定页面通过code获取token的接口默认传递了参数token = login
	// if(!data.token){//其他业务接口传递过来的参数中无token
	//     token = uni.getStorageSync(this.sessionKey);//参数中无token时在本地缓存中获取
	//     console.log("当前token:" + token);
	//     if(!token){//本地无token需重新登录(退出时清缓存token)
	//         _self.login(backpage, backtype);
	//         return;
	//     }else{
	//         data.token = token;
	//     }
	// }
	var timestamp = Date.parse(new Date()); //时间戳
	data["timestamp"] = timestamp;

	//请求方式:GET或POST(POST需配置header: {'content-type' : "application/x-www-form-urlencoded"},)
	// if(method){
	//     method = method.toUpperCase();//小写改为大写
	//     if(method=="POST"){
	//         header = {'content-type' : "application/x-www-form-urlencoded"};
	//     }else{
	//         header = {'content-type' : "application/json"};
	//     }
	// }else{
	//     method = "GET";
	//     header = {'content-type' : "application/json"};
	// }
	//用户交互:加载圈
	// if (!hideLoading) {
	//     uni.showLoading({title:'请稍后...'});
	// }

	//网络请求
	uni.request({
		url: url,
		method: method,
		header: header,
		data: sign(data),
		success: res => {
			// if (!hideLoading) {
			//     uni.hideLoading();
			// }  

			if (res.statusCode && res.statusCode != 200) { 
				//api错误 						
				console.error("接口异常",res.statusCode);
				const errorData = {
					url: url,
					statusCode: res.statusCode,
					method: method,
					header: header,
					data, 
					token: store.state.token
				};
				console.info('接口信息:' + JSON.stringify(errorData, null, 2));
				uni.showToast({
					// content:"" + res.errMsg
					// showCancel:false,
					position: 'bottom',
					icon: "none",
					title: "接口异常 error:" + res.statusCode,
					duration: 3000
				});
				// 上报错误日志
				log.error("接口错误 error:" + JSON.stringify(errorData, null, 2))
				return;
			} else {
				typeof param.success == "function" && param.success(res.data);
				return JSON.stringify(res.data);
			}

			// if (res.data.code) {//返回结果码code判断:0成功,1错误,-1未登录(未绑定/失效/被解绑)
			//     if (res.data.code == "-1") {
			//         _self.login(backpage, backtype);
			//         return;
			//     }
			//     if (res.data.code != "0") {
			//         uni.showModal({
			//             showCancel:false,
			//             content:"" + res.data.msg
			//         });
			//         return;
			//     }
			// } else{
			//     uni.showModal({
			//         showCancel:false,
			//         content:"No ResultCode:" + res.data.msg
			//     });
			//     return;
			// }
			// typeof param.success == "function" && param.success(res.data);
		},
		fail: (e) => {
			uni.showModal({
				content: "" + e.errMsg
			});
			typeof param.fail == "function" && param.fail(e.data);
		},
	});
}


export default sendRequest